package com.evan.review.launchmode;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.evan.review.R;

/**
 * 启动模式演示的入口Activity
 * 用于展示四种不同的启动模式
 */
public class LaunchModeActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "LaunchModeActivity";
    private TextView tvTaskInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch_mode);
        
        initViews();
        updateTaskInfo();
        
        Log.d(TAG, "onCreate: " + this.toString() + ", taskId: " + getTaskId());
    }
    
    private void initViews() {
        tvTaskInfo = findViewById(R.id.tv_task_info);
        
        Button btnStandard = findViewById(R.id.btn_standard);
        Button btnSingleTop = findViewById(R.id.btn_singletop);
        Button btnSingleTask = findViewById(R.id.btn_singletask);
        Button btnSingleInstance = findViewById(R.id.btn_singleinstance);
        Button btnStandardWithFlag = findViewById(R.id.btn_standard_with_flag);
        
        btnStandard.setOnClickListener(this);
        btnSingleTop.setOnClickListener(this);
        btnSingleTask.setOnClickListener(this);
        btnSingleInstance.setOnClickListener(this);
        btnStandardWithFlag.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        int id = v.getId();
        
        if (id == R.id.btn_standard) {
            startStandardActivity();
        } else if (id == R.id.btn_singletop) {
            startSingleTopActivity();
        } else if (id == R.id.btn_singletask) {
            startSingleTaskActivity();
        } else if (id == R.id.btn_singleinstance) {
            startSingleInstanceActivity();
        } else if (id == R.id.btn_standard_with_flag) {
            startStandardActivityWithFlag();
        }
    }
    
    /**
     * 启动标准模式的Activity
     */
    private void startStandardActivity() {
        Intent intent = new Intent(this, StandardActivity.class);
        startActivity(intent);
    }
    
    /**
     * 启动栈顶复用模式的Activity
     */
    private void startSingleTopActivity() {
        Intent intent = new Intent(this, SingleTopActivity.class);
        startActivity(intent);
    }
    
    /**
     * 启动栈内复用模式的Activity
     */
    private void startSingleTaskActivity() {
        Intent intent = new Intent(this, SingleTaskActivity.class);
        startActivity(intent);
    }
    
    /**
     * 启动单实例模式的Activity
     */
    private void startSingleInstanceActivity() {
        Intent intent = new Intent(this, SingleInstanceActivity.class);
        startActivity(intent);
    }
    
    /**
     * 使用Intent标志位启动标准模式Activity
     * 演示如何通过Intent Flag动态设置启动模式
     */
    private void startStandardActivityWithFlag() {
        Intent intent = new Intent(this, StandardActivity.class);
        // 通过Flag设置启动模式为singleTop
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    

        startActivity(intent);
    }
    
    /**
     * 更新任务栈信息显示
     */
    private void updateTaskInfo() {
        StringBuilder sb = new StringBuilder();
        sb.append("当前Activity: ").append(this.getClass().getSimpleName()).append("\n");
        sb.append("实例Hash: ").append(this.hashCode()).append("\n");
        sb.append("任务栈ID: ").append(getTaskId()).append("\n");
        
        tvTaskInfo.setText(sb.toString());
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d(TAG, "onNewIntent: " + this.toString() + ", taskId: " + getTaskId());
        updateTaskInfo();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: " + this.toString() + ", taskId: " + getTaskId());
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: " + this.toString() + ", taskId: " + getTaskId());
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: " + this.toString() + ", taskId: " + getTaskId());
    }
} 